package com.whisperarts.financer.app.utils;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IWorkbenchWindow;

import com.whisperarts.financer.app.editors.TransactionsEditor;
import com.whisperarts.financer.app.views.CategoryView;
import com.whisperarts.financer.app.views.NavigatorView;
import com.whisperarts.financer.dao.AccountsDAO;
import com.whisperarts.financer.model.account.Account;
import com.whisperarts.financer.model.account.AccountsCategory;
import com.whisperarts.financer.model.transaction.Category;

public class UIUtils {
	private static final TreeViewer viewer;
	private static final TreeViewer categoryViewer;
	
	static {
		viewer = NavigatorView.getViewer();
		categoryViewer = CategoryView.getViewer();		
	}
	
	public static void refreshNode(Object... nodes) {
		if (nodes.length == 0) {
			viewer.refresh();
			return;
		}
		for (Object node : nodes) {
			viewer.refresh(node);
			int expandLevel = node instanceof Account ? 0 : 1;
			viewer.expandToLevel(node, expandLevel);
		}
	}
	
	public static void refreshCategoryNode(Category... nodes) {
		if (nodes.length == 0) {
			categoryViewer.refresh();
		} else {
			for (Category node : nodes) {
				if (node.getId() == 0) {
					categoryViewer.refresh();
					break;
				}
				categoryViewer.refresh(node);
			}
		}
		categoryViewer.expandAll();
	}
	
	public static void refreshEditor() {
		
	}
	
	public static boolean checkAccountName(String name) {
		for (AccountsCategory ac : AccountsDAO.getAccountsCategories()) {
			for (Account account : ac.getAccounts()) {
				if (stringsEquals(name, account.getName()))
					return true;
			}
		}
		return false;
	}
	
	public static TreeViewer getTreeViewer() {
		return viewer;
	}

	public static boolean checkCategoryName(String name) {
		for (Category category: AccountsDAO.getCategories().values()) {
			if (stringsEquals(name, category.getName())) 
				return true;
		}
		return false;
	}
	
	private static boolean stringsEquals(String str1, String str2) {
		str1 = str1.trim();
		str2 = str2.trim();
		return str1.equalsIgnoreCase(str2);
	}

//	public static void refreshCategoryNode(Category category) {
//		if (category.getId() == 0)
//			categoryViewer.refresh();		
//		else 
//			categoryViewer.refresh(category);
//		categoryViewer.expandToLevel(3);
//	}
	
	public static void updateActiveEditor(IWorkbenchWindow window, Account account) {
		for (IEditorReference editorRef : window.getActivePage().getEditorReferences()) {
			if (editorRef.getName().equals(account.getName())) {
				IEditorPart editor = editorRef.getEditor(false);
				if (editor != null && editor instanceof TransactionsEditor) {
					TransactionsEditor txEditor = (TransactionsEditor) editor;
					txEditor.getViewer().refresh();
				}
			}
		}
	}

	public static void closeEditor(IWorkbenchWindow window, Account account) {
		for (IEditorReference editorRef : window.getActivePage().getEditorReferences()) {
			if (editorRef.getName().equals(account.getName())) {
				IEditorPart editor = editorRef.getEditor(false);
				if (editor != null) {
					editor.getEditorSite().getPage().closeEditor(editor, false);
				}
			}
		}
	}
}
